<template>
	<div class="m-view-onlineSetting m-window">
		<div class="m-condition">
			<el-row :gutter="10">
				<el-form label-width="80px" :inline-message="true" label-position="left">
					<el-col :span="2" :offset="12">
						<el-button type="primary" @click="search(1)">查询</el-button>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="addNewSetting">新增</el-button>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="editSetting">编辑</el-button>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="importSetting">导入</el-button>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="extendOnline">扩展</el-button>
					</el-col>
					<el-col :span="2">
						<el-button type="primary" @click="batchUpload">上传sap</el-button>
					</el-col>
				</el-form>
			</el-row>
			<el-row :gutter="10">
				<el-form :inline-message="true" label-position="left">
					<el-col :span="7">
						<el-form-item label="供应商编码/名称" label-width="120px">
							<el-input v-model="searchValue" placeholder="请输入供应商编码/名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="7">
						<el-form-item label="公司编码/名称" label-width="110px">
							<el-input v-model="companyValue" placeholder="请输入公司编码/名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="状态" class="rightItem" label-width="60px">
							<el-select value-key="id" v-model="uploadFlag" placeholder="请选择状态" clearable>
								<el-option
									v-for="item in stateOptions"
									:key="item.value"
									:label="item.name"
									:value="item.value"
								></el-option>
							</el-select>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
		</div>
		<div class="m-agTable m-top2 m-bottom" v-loading="loading">
			<agGridComponents
				ref="onlineSetingGrid"
				:getGridOptions="gridOptions"
				:getColumnDefs="columnDefs"
				:getRowSelection="'multiple'"
				:isColFit="true"
			></agGridComponents>
		</div>
		<div class="m-page">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="pageInfo.current"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageInfo.size"
				layout="total, sizes, prev, pager, next, jumper"
				:total="pageInfo.total"
			></el-pagination>
		</div>

		<!--新增/编辑弹框 -->
		<el-dialog
			:title="`${title}上线配置`"
			v-if="dialogVisible"
			:visible.sync="dialogVisible"
			width="50%"
			:before-close="handleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srm-active-supplier
				:dialogVisible="selectedDialogVisible"
				:layout="layout"
				@dialogClose="selectedDialogVisible=false"
				@setSelectData="setSelectData"
				v-if="selectedDialogVisible"
			></srm-active-supplier>
			<el-form
				:model="ruleForm"
				ref="ruleForm"
				label-width="120px"
				class="demo-ruleForm"
				:rules="rules"
				v-loading="formLoading"
			>
				<el-form-item label="配置方式" prop="setType" v-if="title=='新增'">
					<el-radio-group v-model="ruleForm.setType" @change="radioChange">
						<el-radio :label="1">按公司</el-radio>
						<el-radio :label="2">按供应商</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="公司" prop="companyCodes" v-if="ruleForm.setType==2" class="flexItem">
					<el-select
						v-model="ruleForm.companyCodes"
						placeholder="请选择公司"
						filterable
						clearable
						multiple
						@focus="focusMethod(1)"
						ref="companyInput"
						:collapse-tags="collapseTags"
					>
						<el-option
							v-for="item in comOptions"
							:key="item.companyCode"
							:label="item.companyName"
							:value="item.companyCode"
						></el-option>
					</el-select>
					<el-checkbox v-model="checked" v-if="comOptions.length>0">全部</el-checkbox>
				</el-form-item>
				<el-form-item label="公司" prop="companyCode" v-if="title=='编辑'">
					<el-input v-model="ruleForm.companyName" disabled></el-input>
				</el-form-item>
				<el-form-item label="公司" prop="companyName" v-if="ruleForm.setType==1">
					<el-input
						placeholder="请选择公司"
						v-model="ruleForm.companyName"
						clearable
						readonly
						@focus="showSelectDialog('company')"
						ref="companyInput"
					>
						<el-button slot="append" icon="el-icon-search" @click="showSelectDialog('company')"></el-button>
						<!-- <span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
                            <i class="el-input_icon el-icon-circle-close el-input__clear"></i>
						</span>-->
					</el-input>
				</el-form-item>
				<el-form-item label="供应商" prop="supplierCodes" v-if="ruleForm.setType==1" class="flexItem">
					<el-select
						v-model="ruleForm.supplierCodes"
						filterable
						clearable
						multiple
						placeholder="请选择供应商"
						@focus="focusMethod(2)"
						@clear="supplierOptions=allSupplier"
						:filter-method="filterSupplier"
						ref="supplierInput"
						:collapse-tags="collapseTags"
					>
						<el-option
							v-for="item in supplierOptions"
							:key="item.supplierCode"
							:label="item.supplierName"
							:value="item.supplierCode"
						>
							<span style="float: left">{{ item.supplierName }}</span>
							<span style="float: right; color: #8492a6; font-size: 13px">{{ item.supplierCode }}</span>
						</el-option>
					</el-select>
					<el-checkbox v-model="checked" v-if="supplierOptions.length>0">全部</el-checkbox>
					<!-- <el-input v-model="ruleForm.supplierName" disabled v-if="title=='编辑'"></el-input> -->
				</el-form-item>
				<el-form-item label="供应商" prop="supplierCode" v-if="title=='编辑'">
					<el-input v-model="ruleForm.supplierName" disabled></el-input>
				</el-form-item>
				<el-form-item label="供应商" prop="supplierName" v-if="ruleForm.setType==2">
					<el-input
						placeholder="请选择供应商"
						v-model="ruleForm.supplierName"
						clearable
						readonly
						ref="supplierInput"
						@focus="showSelectDialog('supplier')"
					>
						<el-button slot="append" icon="el-icon-search" @click="showSelectDialog('supplier')"></el-button>
						<!-- <span slot="suffix" @click="inputClearSon" class="m-cleanInput" v-show="inputClearBut">
                        <i class="el-input_icon el-icon-circle-close el-input__clear"></i>
						</span>-->
					</el-input>
				</el-form-item>
				<el-form-item label="订单模块" prop="orderEnableFlag">
					<el-select v-model="ruleForm.orderEnableFlag" ref="orderForm">
						<el-option
							v-for="item in useSateOption"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="订单启用日期" prop="orderEnableDate" v-if="ruleForm.orderEnableFlag==1">
					<el-date-picker
						v-model="ruleForm.orderEnableDate"
						type="date"
						placeholder="订单启用日期"
						value-format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="对账模块" prop="accountEnableFlag">
					<el-select v-model="ruleForm.accountEnableFlag">
						<el-option
							v-for="item in useSateOption"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="对账启用日期" prop="accountEnableDate" v-if="ruleForm.accountEnableFlag==1">
					<el-date-picker
						v-model="ruleForm.accountEnableDate"
						type="date"
						placeholder="对账启用日期"
						value-format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="handleClose" :disabled.sync="btnLoading">取消</el-button>
				<el-button type="primary" @click="saveData" :loading="btnLoading">保存</el-button>
				<el-button type="primary" @click="saveData(1)" :loading="btnLoading">保存与继续</el-button>
			</span>
		</el-dialog>
		<!-- 扩展上线配置弹窗 -->
		<el-dialog
			title="上线配置扩展"
			v-if="extendVisible"
			:visible.sync="extendVisible"
			width="50%"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<el-form
				:model="extendForm"
				ref="extendForm"
				label-width="120px"
				class="demo-ruleForm"
				:rules="rules"
				v-loading="formLoading"
			>
				<el-form-item label="供应商" prop="supplierCode">
					<el-input v-model="extendForm.supplierName" disabled></el-input>
				</el-form-item>
				<el-form-item label="公司" prop="companyCodes">
					<el-select v-model="extendForm.companyCodes" placeholder="请选择公司" ref="content" multiple>
						<el-option
							v-for="item in comOptions"
							:key="item.id"
							:label="item.companyName"
							:value="item.companyCode"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="订单模块" prop="orderEnableFlag">
					<el-select v-model="extendForm.orderEnableFlag">
						<el-option
							v-for="item in useSateOption"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="订单启用日期" prop="orderEnableDate" v-if="extendForm.orderEnableFlag==1">
					<el-date-picker
						v-model="extendForm.orderEnableDate"
						type="date"
						placeholder="订单启用日期"
						value-format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
				<el-form-item label="对账模块" prop="accountEnableFlag">
					<el-select v-model="extendForm.accountEnableFlag">
						<el-option
							v-for="item in useSateOption"
							:key="item.value"
							:label="item.label"
							:value="item.value"
						></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="对账启用日期" prop="accountEnableDate" v-if="extendForm.accountEnableFlag==1">
					<el-date-picker
						v-model="extendForm.accountEnableDate"
						type="date"
						placeholder="对账启用日期"
						value-format="yyyy-MM-dd"
					></el-date-picker>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="extendVisible=false" :disabled.sync="btnLoading">取消</el-button>
				<el-button type="primary" @click="saveExtendData" :loading="btnLoading">保存</el-button>
			</span>
		</el-dialog>
		<!-- 导入上线配置 -->
		<el-dialog
			title="导入上线配置"
			v-if="importDialogVisible"
			:visible.sync="importDialogVisible"
			width="50%"
			:before-close="importHandleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<import-file
				:moduleName="'onlineSet'"
				@setImportData="getImportData"
				:closeImportDialog="importDialogVisible"
				:tempUrl="tempUrl"
			></import-file>
			<span slot="footer" class="dialog-footer">
				<el-button @click="importHandleClose" :disabled="btnLoading">取消</el-button>
				<el-button type="primary" @click="saveImportData" :loading="btnLoading">导入</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
import agGridComponents from '@/components/agGridComponents'
import authInfo from '@/public/authInfo'
import Msg from '@/public/message'
import ImportFile from "@/components/importFile"
import fileStream from "@/public/fileStream"
import SrmActiveSupplier from "./common/srmActiveSupplier"
export default {
	data () {
		return {
			checked: false,
			// 供应商编码/名称 
			searchValue: '',
			// 公司编码/名称
			companyValue: '',
			//   上传状态
			uploadFlag: '',
			// 分页相关
			pageInfo: {
				current: 1,
				size: 30,
				total: 0
			},
			layout: 'supplier',
			// 新增弹窗显示状态
			dialogVisible: false,
			// 扩展弹窗显示状态
			extendVisible: false,
			// 选择数据弹窗
			selectedDialogVisible: false,
			// 弹窗标题
			title: '新增',
			// 弹窗表单信息
			ruleForm: {
				companyCode: '',
				orderEnableFlag: 1,
				orderEnableDate: '',
				accountEnableFlag: 1,
				accountEnableDate: '',
				supplierCode: '',
				supplierName: '',
				companyName: 'ffff',
				setType: 1,
				companyCodes: [],
				supplierCodes: []
			},
			// 扩展表单信息
			extendForm: {
				supplierCode: '',
				supplierName: '',
				orderEnableFlag: 1,
				orderEnableDate: '',
				accountEnableFlag: 1,
				accountEnableDate: '',
				companyCodes: []
			},
			// 状态下拉
			useSateOption: [
				{ label: '启用', value: 1 },
				{ label: '禁用', value: 0 }
			],
			// 公司信息下拉
			comOptions: [],
			// 供应商下拉
			supplierOptions: [],
			allSupplier: [],
			gridOptions: {},
			columnDefs: [],
			// 表单验证 
			rules: {
				companyCodes: [
					{ required: true, message: '公司不能为空', trigger: 'blur' }
				],
				companyCode: [
					{ required: true, message: '公司不能为空', trigger: 'blur' }
				],
				companyName: [
					{ required: true, message: '公司不能为空', trigger: 'change' }
				],
				supplierName: [
					{ required: true, message: '供应商不能为空', trigger: 'change' }
				],
				supplierCode: [
					{ required: true, message: '供应商不能为空', trigger: 'blur' }
				],
				supplierCodes: [
					{ required: true, message: '供应商不能为空', trigger: 'blur' }
				],
				orderEnableDate: [
					{ required: true, message: '订单启用日期不能为空', trigger: 'blur' }
				],
				accountEnableDate: [
					{ required: true, message: '对账启用日期不能为空', trigger: 'blur' }
				]
			},
			formLoading: false,
			// 导入弹窗相关
			importDialogVisible: false,
			// 解析表格获得的数据
			importData: [],
			// 上线配置模板下载路径  
			tempUrl: '',
			pickerOptions: {
				disabledDate: (time) => {
					return time.getTime() < (new Date().getTime() - 3600 * 1000 * 24);
				}
			},
			//  按钮加载状态
			btnLoading: false,
			loading: false,
			stateOptions: [
				{ value: 1, name: '已上传' },
				{ value: 0, name: '未上传' }
			],
			collapseTags: false
		}
	},
	watch: {
		checked (newVal) {
			const { setType } = this.ruleForm
			if (newVal && setType == 1) {
				this.ruleForm.supplierCodes = this.allSupplier.map(item => {
					return item.supplierCode
				})
				this.collapseTags = true
			} else if (newVal && setType == 2) {
				this.ruleForm.companyCodes = this.comOptions.map(item => {
					return item.companyCode
				})
				this.collapseTags = true
			} else if (!newVal) {
				this.ruleForm.supplierCodes = []
				this.ruleForm.companyCodes = []
				this.collapseTags = false
			}
		}
	},
	methods: {
		// 显示选择公司或供应商弹窗
		showSelectDialog (type) {
			this.layout = type
			this.selectedDialogVisible = true
			this.supplierOptions = []
			this.comOptions = []
			this.checked = false
		},
		// 设置选项数据
		setSelectData (row) {
			const { companyCode, companyName, supplierName, supplierCode } = row
			const { setType } = this.ruleForm
			if (setType == 1) {
				this.ruleForm.companyCode = companyCode
				this.ruleForm.companyName = companyName
				this.ruleForm.supplierCodes = []
				this.ruleForm.supplierCode = ''
				this.ruleForm.supplierName = ''
				this.filterCompanyData(companyCode, 1).then(rows => {
					if (Array.isArray(rows) && rows.length == 0) {
						Msg.showPopMsg('所选公司无可配置供应商!')
						return
					}
					let options = []
					rows.forEach(row => {
						options.push({
							supplierCode: row.code,
							supplierName: row.name
						})
					})
					this.supplierOptions = options
					this.allSupplier = options
				}).catch(err => {
					Msg.showErrorMsg(err)
				})
			} else if (setType == 2) {
				this.ruleForm.supplierCode = supplierCode
				this.ruleForm.supplierName = supplierName
				this.ruleForm.companyCodes = []
				this.ruleForm.companyCode = ''
				this.ruleForm.companyName = ''
				this.filterCompanyData(supplierCode, 2).then(rows => {
					if (Array.isArray(rows) && rows.length == 0) {
						Msg.showPopMsg('所选供应商无可配置公司!')
						return
					}
					let options = []
					rows.forEach(row => {
						options.push({
							companyCode: row.code,
							companyName: row.name
						})
					})
					this.comOptions = options
				}).catch(err => {
					Msg.showErrorMsg(err)
				})
			}
		},
		// 配置方式变更回调
		radioChange () {
			this.ruleForm.companyCode = ''
			this.ruleForm.companyName = ''
			this.ruleForm.supplierCode = ''
			this.ruleForm.supplierName = ''
			this.checked = false
		},
		// 筛选所关联供应商或公司数据
		filterCompanyData (code, type) {
			return new Promise((resolve, reject) => {
				let params = {}
				if (type == 1) {
					params.companyCode = code
				} else if (type == 2) {
					params.supplierCode = code
				}
				this.$http.filterAuthData(params).then(({ data }) => {
					if (data.code == 1) {
						resolve(data.rows)
					} else {
						reject(data.info)
					}
				})
			}).catch(err => {
				reject(err.data.message)
			})
		},
		//   查询列表
		search (flag) {
			if (flag) this.pageInfo.current = 1
			const { current, size } = this.pageInfo
			let params = {
				likeParams: {
					supplier: this.searchValue,
					company: this.companyValue,
					uploadFlag: this.uploadFlag
				},
				current,
				size
			}
			this.loading = true
			this.$http.getOnlineSettingList(params).then(({ data }) => {
				this.loading = false
				if (data.code == 1) {
					this.$refs.onlineSetingGrid.gridOptions.api.setRowData(data.rows)
					this.pageInfo.total = data.total
				} else {
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				this.loading = false
				Msg.showErrorMsg(err.data.message)
			})
		},
		// 新增配置
		addNewSetting () {
			this.title = "新增"
			this.dialogVisible = true
			this.ruleForm = {
				companyCode: '',
				companyName: '',
				orderEnableFlag: 1,
				orderEnableDate: new Date().Format('yyyy-MM-dd'),
				accountEnableFlag: 1,
				accountEnableDate: new Date().Format('yyyy-MM-dd'),
				supplierCode: '',
				supplierName: '',
				companyCodes: [],
				supplierCodes: [],
				setType: 1
			}
		},
		// 编辑配置
		editSetting () {
			let row = this.$refs.onlineSetingGrid.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要编辑的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			if (row.length > 1) {
				this.$alert("提示：只能选择单条数据进行编辑操作！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			this.title = "编辑"
			const {
				companyCode,
				companyName,
				supplierCode,
				accountEnableDate,
				orderEnableDate,
				orderEnableFlag,
				accountEnableFlag,
				supplierName, id } = row[0]
			// 获取配置详情数据   
			this.ruleForm = {
				companyCode,
				supplierCode,
				accountEnableDate,
				orderEnableDate,
				orderEnableFlag,
				accountEnableFlag,
				supplierName,
				companyName,
				id
			}
			this.dialogVisible = true
		},
		// 扩展上线配置
		extendOnline () {
			let row = this.$refs.onlineSetingGrid.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要扩展的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			if (row.length > 1) {
				this.$alert("提示：只能选择单条数据进行操作！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			const {
				companyCode,
				supplierCode,
				accountEnableDate,
				orderEnableDate,
				orderEnableFlag,
				accountEnableFlag,
				supplierName, id } = row[0]
			// 获取配置详情数据   
			this.extendForm = {
				companyCodes: [],
				supplierCode,
				accountEnableDate,
				orderEnableDate,
				orderEnableFlag,
				accountEnableFlag,
				supplierName,
				id
			}
			this.filterCompanyData(supplierCode, 2).then(rows => {
				if (Array.isArray(rows) && rows.length == 0) {
					Msg.showPopMsg('当前供应商无可配置公司!')
					return
				}
				let options = []
				rows.forEach(row => {
					options.push({
						companyCode: row.code,
						companyName: row.name
					})
				})
				this.comOptions = options
				this.extendForm.companyCodes = this.comOptions.map(com => {
					return com.companyCode
				})
			}).catch(err => {
				Msg.showErrorMsg(err)
			})
			this.extendVisible = true
		},
		// 导入数据
		importSetting () {
			this.importDialogVisible = true
		},
		// 每页条数改变回调
		handleSizeChange (val) {
			this.pageInfo.size = val
			this.search(1)
		},
		// 页码改变
		handleCurrentChange (val) {
			this.pageInfo.current = val
			this.search()
		},
		// 上一页
		handlePrevClick (val) {
			this.pageInfo.current = val
			this.search()
		},
		// 下一页
		handleNextClick (val) {
			this.pageInfo.current = val
			this.search()
		},
		// 弹窗关闭
		handleClose () {
			this.$refs.ruleForm.resetFields()
			this.dialogVisible = false
		},
		// 上线扩展保存
		saveExtendData () {
			this.$refs["extendForm"].validate(valid => {
				if (valid) {
					const { companyCodes, supplierCode,
						accountEnableDate,
						orderEnableDate,
						orderEnableFlag,
						accountEnableFlag,
						supplierName, } = this.extendForm
					let params = []
					let orderDate = new Date(orderEnableDate).Format('yyyy-MM-dd 00:00:00')
					let accountDate = new Date(accountEnableDate).Format('yyyy-MM-dd 00:00:00')
					this.comOptions.forEach(com => {
						companyCodes.forEach(code => {
							if (com.companyCode == code) {
								params.push({
									...com,
									supplierCode,
									accountEnableDate: accountEnableFlag == 1 ? accountDate : '',
									orderEnableDate: orderEnableFlag == 1 ? orderDate : '',
									orderEnableFlag,
									accountEnableFlag,
									supplierName,
									creator: this.$global.user.employeeName,
									creatorId: this.$global.user.id
								})
							}
						})
					})
					this.$http.extendOnlineSet(params).then(({ data }) => {
						if (data.code == 1) {
							this.search(1)
							this.extendVisible = false
						} else {
							Msg.showErrorMsg(data.info)
						}
					}).catch(err => {
						Msg.showErrorMsg(err.data.message)
					})
				}
			})
		},
		// 新增上线保存数据
		saveNewOnline (flag) {
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					const {
						companyCodes,
						supplierCodes,
						accountEnableDate,
						orderEnableDate,
						orderEnableFlag,
						setType,
						supplierCode,
						companyCode,
						accountEnableFlag } = this.ruleForm
					let orderDate = new Date(orderEnableDate).Format('yyyy-MM-dd 00:00:00')
					let accountDate = new Date(accountEnableDate).Format('yyyy-MM-dd 00:00:00')
					let params = []
					if (setType == 1) {
						this.allSupplier.forEach(sup => {
							supplierCodes.forEach(code => {
								if (sup.supplierCode == code) {
									params.push({
										...sup,
										accountEnableDate: accountEnableFlag == 1 ? accountDate : '',
										orderEnableDate: orderEnableFlag == 1 ? orderDate : '',
										orderEnableFlag,
										companyCode,
										accountEnableFlag,
										creator: this.$global.user.employeeName,
										creatorId: this.$global.user.id
									})
								}
							})
						})
					} else if (setType == 2) {
						this.comOptions.forEach(com => {
							companyCodes.forEach(code => {
								if (com.companyCode == code) {
									params.push({
										...com,
										accountEnableDate: accountEnableFlag == 1 ? accountDate : '',
										orderEnableDate: orderEnableFlag == 1 ? orderDate : '',
										orderEnableFlag,
										supplierCode,
										accountEnableFlag,
										creator: this.$global.user.employeeName,
										creatorId: this.$global.user.id
									})
								}
							})
						})
					}
					this.btnLoading = true
					this.$http.batchOnlineSetting(params).then(({ data }) => {
						this.btnLoading = false
						if (data.code == 1) {
							Msg.showSuccessMsg('新增成功!')
							if (flag == 1) {
								this.$refs['ruleForm'].resetFields()
							} else {
								this.handleClose()
							}
							this.search(1)
						} else {
							Msg.showErrorMsg(data.info)
						}
					}).catch(err => {
						this.btnLoading = false
						Msg.showErrorMsg(err.data.message)
					})
				}
			})
		},
		// 保存数据
		saveData (flag) {
			if (this.title == '新增') {
				this.saveNewOnline(flag)
			} else {
				this.saveEditData()
			}
		},
		// 编辑保存数据
		saveEditData () {
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					const { accountEnableDate, orderEnableDate, orderEnableFlag, accountEnableFlag } = this.ruleForm
					let orderDate = new Date(orderEnableDate).Format('yyyy-MM-dd 00:00:00')
					let accountDate = new Date(accountEnableDate).Format('yyyy-MM-dd 00:00:00')
					let params = {
						...this.ruleForm,
						orderEnableDate: orderEnableFlag == 1 ? orderDate : '',
						accountEnableDate: accountEnableFlag == 1 ? accountDate : ''
					}
					params.modifyer = this.$global.user.employeeName
					params.modifyerId = this.$global.user.id
					this.btnLoading = true
					this.$http.saveOnlineSetting(params).then(({ data }) => {
						this.btnLoading = false
						if (data.code == 1) {
							Msg.showSuccessMsg('保存成功')
							this.handleClose()
							this.search(1)
						} else if (data.code == 2) {
							this.handleClose()
							this.search(1)
							Msg.showPopMsg(data.info)
						} else {
							Msg.showErrorMsg(data.info)
						}
					}).catch(err => {
						this.btnLoading = false
						Msg.showErrorMsg(err.data.message)
					})
				}
			})
		},
		// 获取当前账户权限公司
		getAuthCompany () {
			authInfo.getAuthInfo().then(companyData => {
				this.comOptions = companyData
			}).catch(err => {
				this.comOptions = []
				Msg.showErrorMsg(err)
			})
		},
		// 获取当前公司所有供应商
		getAuthSuppliers (companyCode) {
			this.formLoading = true
			this.$http.getAuthSupplierByCompanyCode({ name: companyCode }).then(({ data }) => {
				this.formLoading = false
				if (data.code == 1) {
					this.supplierOptions = data.rows
					this.allSupplier = data.rows
				} else {
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				this.formLoading = false
				Msg.showErrorMsg(err.data.message)
			})
		},
		// 选择公司回调
		companyChange (code) {
			// if (code) {
			// 	this.getAuthSuppliers(code)
			// }
		},
		// 筛选供应商
		filterSupplier (value) {
			if (value != '') {
				this.supplierOptions = this.allSupplier.filter(item => item.supplierCode.indexOf(value) > -1 || item.supplierName.indexOf(value) > -1)
			} else {
				this.supplierOptions = this.allSupplier
			}
		},
		// 供应商下拉框获取焦点
		focusMethod (type) {
			const { companyCode, supplierCode } = this.ruleForm
			if (type == 2) {
				if (!companyCode) {
					Msg.showPopMsg('请先选择公司!')
					this.$refs.supplierInput.blur()
					this.$refs.orderForm.focus()
				}
			} else if (type == 1) {
				if (!supplierCode) {
					Msg.showPopMsg('请先选择供应商!')
					this.$refs.companyInput.blur()
					this.$refs.orderForm.focus()
				}
			}
		},
		// 关闭导入模板弹窗
		importHandleClose (done) {
			//   done();
			this.importDialogVisible = false;
		},
		// 保存导入的数据
		saveImportData () {
			if (this.importData.length == 0) {
				Msg.showPopMsg('导入数据为空或未按指定模板填写！')
				return
			}
			this.btnLoading = true
			this.$http.batchOnlineSetting(this.importData).then(({ data }) => {
				this.btnLoading = false
				if (data.code == 1) {
					Msg.showSuccessMsg('批量导入成功')
					this.importHandleClose()
					this.search(1)
				} else {
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				this.btnLoading = false
				Msg.showErrorMsg(err.data.message)
			})
		},
		// 获取解析表格得到的数据
		getImportData (data) {
			this.importData = []
			data.forEach(item => {
				const { orderEnableDate, accountEnableDate } = item
				this.importData.push({
					...item,
					accountEnableFlag: item.accountEnableFlag == '禁用' ? 0 : 1,
					orderEnableFlag: item.orderEnableFlag == '禁用' ? 0 : 1,
					orderEnableDate: orderEnableDate ? new Date(orderEnableDate).Format("yyyy-MM-dd 00:00:00") : '',
					accountEnableDate: accountEnableDate ? new Date(accountEnableDate).Format("yyyy-MM-dd 00:00:00") : '',
					creator: this.$global.user.employeeName,
					creatorId: this.$global.user.id
				})
			})
		},
		// 获取上线配置导入模板
		getContractTemp () {
			fileStream.getPrintTempUrl("onlineSettingImport").then(url => {
				this.tempUrl = url
			})
		},
		// 批量上传
		batchUpload () {
			let row = this.$refs.onlineSetingGrid.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要上传的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				})
				return
			}
			let params = []
			row.forEach(item => {
				params.push({
					accountEnableDate: item.accountEnableDate,
					orderEnableDate: item.orderEnableDate,
					orderEnableFlag: item.orderEnableFlag,
					accountEnableFlag: item.accountEnableFlag,
					id: item.id,
					companyCode: item.companyCode,
					supplierCode: item.supplierCode,
					modifyer: this.$global.user.employeeName,
					modifyerId: this.$global.user.id
				})
			})
			this.$http.batchOnlineUpload(params).then(({ data }) => {
				if (data.code == 1) {
					Msg.showSuccessMsg('上传成功')
					this.search(1)
				} else {
					Msg.showErrorMsg(data.info)
				}
			}).catch(err => {
				Msg.showErrorMsg(err.data.message)
			})
		}
	},
	components: {
		agGridComponents,
		ImportFile,
		SrmActiveSupplier
	},
	beforeMount () {
		this.gridOptions = {
			context: {
				componentParent: this
			},
			enableColResize: true,
			suppressRowClickSelection: true,
			onGridReady: function (params) {
				// params.api.sizeColumnsToFit()
			}
		}
		this.columnDefs = [
			{ headerName: "编号", field: "supplierCode", width: 140, checkboxSelection: true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true },
			{ headerName: "供应商名称", field: "supplierName", width: 160 },
			{ headerName: "公司编号", field: "companyCode", width: 90 },
			{ headerName: "公司名称", field: "companyName", width: 160 },
			{
				headerName: "订单模块",
				field: "orderEnableFlag",
				cellRenderer: function (params) {
					if (params.value == "1")
						return '<span class="ag-icon">启用</span>'
					else if (params.value == "0")
						return '<span class="ag-icon">禁用</span>'
				},
				width: 90
			},
			{
				headerName: "订单启用日期",
				field: "orderEnableDate",
				width: 120,
				cellRenderer: function (params) {
					if (params.value && params.value != "")
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{
				headerName: "对账模块",
				field: "accountEnableFlag",
				cellRenderer: function (params) {
					if (params.value == "1")
						return '<span class="ag-icon">启用</span>'
					else if (params.value == "0")
						return '<span class="ag-icon">禁用</span>'
				},
				width: 90
			},
			{
				headerName: "对账启用日期",
				field: "accountEnableDate",
				width: 120,
				cellRenderer: function (params) {
					if (params.value && params.value != "")
						return new Date(Date.parse(params.value.replace(/-/g, "/"))).Format("yyyy-MM-dd");
				}
			},
			{ headerName: "最后修改时间", field: "modifyDate", width: 150 },
			{
				headerName: "上传状态",
				field: "uploadFlag",
				cellRenderer: function (params) {
					if (!params.value)
						return '<span class="ag-icon">未上传</span>'
					else if (params.value == 1)
						return '<span class="ag-icon">已上传</span>'
					else if (params.value == 2)
						return '<span class="ag-icon">失败</span>'
				},
				width: 90
			},
			{ headerName: "最后上传时间", field: "uploadDate", width: 150 },
			{ headerName: "上传日志", field: "uploadMemo", width: 90, tooltipField: 'uploadMemo' }
		]
	},
	created () {
		// this.getAuthCompany()
		this.search(1)
		this.getContractTemp()
	}
};
</script>

<style lang="less" scoped>
.m-view-onlineSetting {
	/deep/.el-form {
		.flexItem {
			/deep/.el-form-item__content {
				display: flex;
				.el-checkbox {
					margin-left: 6px;
					margin-right: 0;
				}
			}
		}
	}
	/deep/.el-dialog {
		.el-dialog__body {
			max-height: 400px;
			overflow: auto;
		}
	}
}
</style>